package vp.remedies.dr.home.fragment;

import java.util.ArrayList;
import java.util.List;

import vp.remedies.dr.home.R;
import vp.remedies.dr.home.activity.TypesofDiseasesActivity;
import vp.remedies.dr.home.db.AndroidSQLiteHelper;
import vp.remedies.dr.home.so.TypesofDiseasesList;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

@SuppressLint({ "ValidFragment", "NewApi" })
public class TypesOfDiseasesMetro2Fragment extends Fragment {

	public TypesOfDiseasesMetro2Fragment() {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View rootView = inflater.inflate(R.layout.types_of_diseases_metro_2,
				container, false);

		List<TypesofDiseasesList> typesOfDiseaseList = TypesOfDiseasesMetro1Fragment.typesOfDiseaseList;
		if (typesOfDiseaseList == null) {
			typesOfDiseaseList = getTypesofDiseasesList();
		}

		LinearLayout linearLayoutType11 = (LinearLayout) rootView
				.findViewById(R.id.type11);
		LinearLayout linearLayoutType12 = (LinearLayout) rootView
				.findViewById(R.id.type12);
		LinearLayout linearLayoutType13 = (LinearLayout) rootView
				.findViewById(R.id.type13);
		LinearLayout linearLayoutType14 = (LinearLayout) rootView
				.findViewById(R.id.type14);
		LinearLayout linearLayoutType15 = (LinearLayout) rootView
				.findViewById(R.id.type15);
		LinearLayout linearLayoutType16 = (LinearLayout) rootView
				.findViewById(R.id.type16);
		LinearLayout linearLayoutType17 = (LinearLayout) rootView
				.findViewById(R.id.type17);
		LinearLayout linearLayoutType18 = (LinearLayout) rootView
				.findViewById(R.id.type18);
		LinearLayout linearLayoutType19 = (LinearLayout) rootView
				.findViewById(R.id.type19);
		LinearLayout linearLayoutType20 = (LinearLayout) rootView
				.findViewById(R.id.type20);
		LinearLayout linearLayoutTypeMoreTypes = (LinearLayout) rootView
				.findViewById(R.id.moretypes);

		final int type11 = ((TypesofDiseasesList) typesOfDiseaseList.get(10))
				.getId();
		final String typeName11 = ((TypesofDiseasesList) typesOfDiseaseList
				.get(0)).getName();
		int id1 = getResources().getIdentifier("i" + type11, "drawable",
				getActivity().getPackageName());

		linearLayoutType11.setBackgroundResource(id1);
		linearLayoutType11.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(getActivity(),
						TypesofDiseasesActivity.class);
				String diseas_type = "" + (type11);
				i.putExtra(TypesofDiseasesActivity.DISEAS_TYPE, diseas_type);
				startActivity(i);
			}
		});

		final int type12 = ((TypesofDiseasesList) typesOfDiseaseList.get(11))
				.getId();
		final String typeName12 = ((TypesofDiseasesList) typesOfDiseaseList
				.get(11)).getName();
		int id2 = getResources().getIdentifier("i" + type12, "drawable",
				getActivity().getPackageName());
		linearLayoutType12.setBackgroundResource(id2);
		linearLayoutType12.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(getActivity(),
						TypesofDiseasesActivity.class);
				String diseas_type = "" + (type12);
				i.putExtra(TypesofDiseasesActivity.DISEAS_TYPE, diseas_type);
				startActivity(i);
			}
		});

		final int type13 = ((TypesofDiseasesList) typesOfDiseaseList.get(12))
				.getId();
		String typeName13 = ((TypesofDiseasesList) typesOfDiseaseList.get(12))
				.getName();
		int id3 = getResources().getIdentifier("i" + type13, "drawable",
				getActivity().getPackageName());
		linearLayoutType13.setBackgroundResource(id3);
		linearLayoutType13.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(getActivity(),
						TypesofDiseasesActivity.class);
				String diseas_type = "" + (type13);
				i.putExtra(TypesofDiseasesActivity.DISEAS_TYPE, diseas_type);
				startActivity(i);
			}
		});

		final int type14 = ((TypesofDiseasesList) typesOfDiseaseList.get(13))
				.getId();
		final String typeName14 = ((TypesofDiseasesList) typesOfDiseaseList
				.get(13)).getName();
		int id4 = getResources().getIdentifier("i" + type14, "drawable",
				getActivity().getPackageName());
		linearLayoutType14.setBackgroundResource(id4);
		linearLayoutType14.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(getActivity(),
						TypesofDiseasesActivity.class);
				String diseas_type = "" + (type14);
				i.putExtra(TypesofDiseasesActivity.DISEAS_TYPE, diseas_type);
				startActivity(i);
			}
		});

		final int type15 = ((TypesofDiseasesList) typesOfDiseaseList.get(14))
				.getId();
		final String typeName15 = ((TypesofDiseasesList) typesOfDiseaseList
				.get(14)).getName();
		int id5 = getResources().getIdentifier("i" + type15, "drawable",
				getActivity().getPackageName());
		linearLayoutType15.setBackgroundResource(id5);
		linearLayoutType15.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(getActivity(),
						TypesofDiseasesActivity.class);
				String diseas_type = "" + (type15);
				i.putExtra(TypesofDiseasesActivity.DISEAS_TYPE, diseas_type);
				startActivity(i);
			}
		});

		final int type16 = ((TypesofDiseasesList) typesOfDiseaseList.get(15))
				.getId();
		final String typeName16 = ((TypesofDiseasesList) typesOfDiseaseList
				.get(15)).getName();
		int id6 = getResources().getIdentifier("i" + type16, "drawable",
				getActivity().getPackageName());
		linearLayoutType16.setBackgroundResource(id6);
		linearLayoutType16.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(getActivity(),
						TypesofDiseasesActivity.class);
				String diseas_type = "" + (type16);
				i.putExtra(TypesofDiseasesActivity.DISEAS_TYPE, diseas_type);
				startActivity(i);
			}
		});

		final int type17 = ((TypesofDiseasesList) typesOfDiseaseList.get(16))
				.getId();
		final String typeName17 = ((TypesofDiseasesList) typesOfDiseaseList
				.get(16)).getName();
		int id7 = getResources().getIdentifier("i" + type17, "drawable",
				getActivity().getPackageName());
		linearLayoutType17.setBackgroundResource(id7);
		linearLayoutType17.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(getActivity(),
						TypesofDiseasesActivity.class);
				String diseas_type = "" + (type17);
				i.putExtra(TypesofDiseasesActivity.DISEAS_TYPE, diseas_type);
				startActivity(i);
			}
		});

		final int type18 = ((TypesofDiseasesList) typesOfDiseaseList.get(17))
				.getId();
		final String typeName18 = ((TypesofDiseasesList) typesOfDiseaseList
				.get(17)).getName();
		int id8 = getResources().getIdentifier("i" + type18, "drawable",
				getActivity().getPackageName());
		linearLayoutType18.setBackgroundResource(id8);
		linearLayoutType18.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(getActivity(),
						TypesofDiseasesActivity.class);
				String diseas_type = "" + (type18);
				i.putExtra(TypesofDiseasesActivity.DISEAS_TYPE, diseas_type);
				startActivity(i);
			}
		});

		final int type19 = ((TypesofDiseasesList) typesOfDiseaseList.get(18))
				.getId();
		final String typeName19 = ((TypesofDiseasesList) typesOfDiseaseList
				.get(18)).getName();
		int id9 = getResources().getIdentifier("i" + type19, "drawable",
				getActivity().getPackageName());

		linearLayoutType19.setBackgroundResource(id9);
		linearLayoutType19.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(getActivity(),
						TypesofDiseasesActivity.class);
				String diseas_type = "" + (type19);
				i.putExtra(TypesofDiseasesActivity.DISEAS_TYPE, diseas_type);
				startActivity(i);
			}
		});

		final int type20 = ((TypesofDiseasesList) typesOfDiseaseList.get(19))
				.getId();
		final String typeName20 = ((TypesofDiseasesList) typesOfDiseaseList
				.get(19)).getName();
		int id10 = getResources().getIdentifier("i" + type20, "drawable",
				getActivity().getPackageName());

		linearLayoutType20.setBackgroundResource(id10);
		linearLayoutType20.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(getActivity(),
						TypesofDiseasesActivity.class);
				String diseas_type = "" + (type20);
				i.putExtra(TypesofDiseasesActivity.DISEAS_TYPE, diseas_type);
				startActivity(i);
			}
		});

		linearLayoutTypeMoreTypes
				.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						Toast.makeText(getActivity(), "Jai Sai Rama",
								Toast.LENGTH_LONG).show();
						Toast.makeText(getActivity(), "Jai Sai Rama",
								Toast.LENGTH_LONG).show();
						Fragment fragment = new TypesOfDiseasesMetro3Fragment();
						FragmentManager fragmentManager = getFragmentManager();
						fragmentManager.beginTransaction()
								.replace(R.id.frame_container, fragment,"TypesOfDiseasesMetro3Fragment")
								.commit();
					}
				});

		return rootView;
	}

	public static List<TypesofDiseasesList> getTypesofDiseasesList() {
		List<TypesofDiseasesList> typesOfDiseaseList = new ArrayList<TypesofDiseasesList>();
		AndroidSQLiteHelper helper = AndroidSQLiteHelper.getInstance();
		/*
		 * Cursor cursor = helper .getCursorRaw(
		 * "SELECT  id , name FROM types_of_diseases_en where ORDERED BY name ASC"
		 * , null);
		 */
		String orderBy = "name" + " ASC";
		Cursor cursor = helper.getReadableDatabase().query(
				"types_of_diseases_en", new String[] { "id", "name" }, null,
				null, null, null, orderBy);

		while (cursor.moveToNext()) {
			TypesofDiseasesList diseasesList = new TypesofDiseasesList();
			int id = cursor.getInt(0);
			String name = cursor.getString(1);
			diseasesList.setId(id);
			diseasesList.setName(name);
			typesOfDiseaseList.add(diseasesList);
		}
		return typesOfDiseaseList;
	}

}
